home *** CD-ROM | disk | FTP | other *** search
- Path: io.com!not-for-mail
- From: jamshid@io.com (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ & macros
- Date: 27 Feb 1996 21:11:20 -0600
- Organization: Illuminati Online, Austin, Texas, USA
- Message-ID: <4h0h4o$7a3@xanadu.io.com>
- References: <DLBxoH.GLw@ariel.cs.yorku.ca> <4eun5f$50c@cloner4.netcom.com> <4gg06d$401m@news-s01.ny.us.ibm.net> <4gioa2$nsb@sun152.spd.dsccc.com>
- NNTP-Posting-Host: xanadu.io.com
-
- In article <4gioa2$nsb@sun152.spd.dsccc.com>,
- Kevin Cline <kcline@sun152.spd.dsccc.com> wrote:
- >[...]
- >Using macros to define functions is a very bad idea. Use inline functions
- >instead. This allows the compiler to decide whether inlining the code
- >is faster than a function call. The compiler knows more about this than
- >the programmer. Also, inline functions calls are typesafe.
- >
- >Macros should only be used for text-processing tricks that improve
- >code maintainability, e.g.
-
- Some of my favorite Stroustrup quotes are in CPL2 4.7 Macros. "first
- rule is: Don't use them if you don't have to. It's been observed that
- almost every macro demonstrates a flaw in the programming language, in
- the program, or in the programmer. [...] expect inferior service from
- tools such as debuggers, cross reference tools, and profilers. [...]
- If you must [...] try not to be too clever." And my favorite, "Using
- macros you design your own private language; it will most likely be
- incomprehensible to others."
-
- > #define FRUIT_LIST \
- > FRUIT(apple), \
- > FRUIT(orange), \
- > FRUIT(pear)
- >
- > #define FRUIT(t) t
- > typedef enum { ITEM_LIST } Fruit;
- > #undef FRUIT
- >
- > // #t produces a quoted string
- > #define FRUIT(t) #t
- > static const char* const FruitNameTable[] = { ITEM_LIST };
- > #undef ITEM
- >
- >When the list gets long, techniques like this can be useful for
- >reducing redundancies in source code. I find that as soon
- >as I have to specify the same thing twice, and the compiler is unable
- >to make sure they match, they won't.
-
- Is the above convenience worth the extra lines of code and comments?
- Is it worth the extra time future maintainers are going to have to
- take figuring out this scheme and verifying in their mind that it does
- what's expected? Wouldn't it take less time to just copy&paste the
- enumerator list and use your editor's keystroke record and playback to
- safely change the items to string literals? Of course, different
- programmers will have different opinions on this, but I wouldn't want
- to use or maintain macro tricks like the above (though I've seen much
- worse -- the "#define GLOBAL extern" trick is especially silly,
- especially when people get fancy and try to add a INIT(value) macro).
-
- Jamshid Afshar
- jamshid@io.com
-
-